#include"orderroledao.h"
#include"../utils/database.h"
#include"QSqlQuery"
#include"../poco/orderRole/mulOrderRole.h"
#include"../poco/orderRole/subOrderRole.h"
#include"../utils/orderrolefectory.h"
#include<QSqlRecord>
#include<QVariant>
#include<QSqlError>
#include<QDebug>

OrderRoleDao::OrderRoleDao()
{
    QSqlQuery result=QSqlQuery(*DataBase::getInstance());
}

//查询指定窗口中的所有订单优惠
vector<OrderRoleBase*> OrderRoleDao::selectAllRole(WindowInfo win)
{
    QString sql=("select * from discount where window_id = '%1'");
    sql = sql.arg(win.id);
    QSqlQuery result=DataBase::getInstance()->exec(sql);
    vector<OrderRoleBase*> ans;

    //for(int i=0;i<result.size();i++)
    while(result.next())
    {
        // 再查询详情表，获得每个优惠对应的具体的商品 （scope）
        int _discountID = result.value("id").toInt();
        QString sql=("select * from discountDetail where discount_id = %1");
        sql = sql.arg(_discountID);
        QSqlQuery result_detail=DataBase::getInstance()->exec(sql);

        vector<int> _scope;
//        for(int i=0;i<result_detail.size();i++)
//        {
//            result_detail.next();
//            _scope.push_back(result.value("food_id").toInt());
//        }
        while(result_detail.next())
            _scope.push_back(result_detail.value("food_id").toInt());

        // 然后填充一个OrderRoleBase


        OrderRoleBase* _orderRole = nullptr;
        int type=result.value("type").toInt();

        InitOrderRoleFectory orf;
        _orderRole=orf.createOrderRole(type);
        if(_orderRole==nullptr)
            _orderRole=new OrderRoleBase;
        _orderRole->id = _discountID;
        _orderRole->type = type;
        _orderRole->value = result.value("value").toDouble();
        _orderRole->description = result.value("description").toString().toStdString();
        _orderRole->scope = _scope;

        ans.push_back(_orderRole);

    }

    return ans;

}
//为某个窗口添加订单优惠信息
bool OrderRoleDao::insert(OrderRoleBase* orderRole,WindowInfo win)
{
    QString sql="insert into discount(id,value,window_id,description,type) "
                "value(%1,%2,%3,'%4',%5)";
    sql=sql.arg(orderRole->id)
           .arg(orderRole->value)
           .arg(win.id)
           .arg(QString::fromStdString(orderRole->description))
           .arg(orderRole->type);

    if(result.exec(sql))
        return true;
    else
    {
        qDebug()<<result.lastError().text();
        return false;
    }

    // 然后 插入详情表
    for(int i=0;i < orderRole->scope.size();i++)
    {
        QString sql="insert into discountDetail(discount_id,food_id) "
                    "value('%1','%2')";
        sql=sql.arg(orderRole->id)
               .arg(orderRole->scope[i]);
        if(!result.exec(sql))
            return false;
    }


}

//删除一个优惠信息
bool OrderRoleDao::del(OrderRoleBase* orderRole,WindowInfo win)
{
    /*
    * 需要提供 1）优惠信息id 2）window id
    * 先删除详情表（多行），然后删除discount的一行
    */
    QString sql0="delete from discountDetail where discount_id = %1";
    sql0=sql0.arg(orderRole->id);

    if(!result.exec(sql0))
    {
        qDebug()<<result.lastError().text();
        return false;
    }

    //-------------------------------

    QString sql="delete from discount where id = %1 and window_id = %2";
    sql=sql.arg(orderRole->id)
           .arg(win.id);

    if(result.exec(sql))
          return true;
    else
    {
        qDebug()<<result.lastError().text();
        return false;
    }


}
/*
1
*/
